package org.daidai.abstrusewriting.homepage.hot.adapters;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.homepage.hot.activity.EditRecommendActivity;
import org.daidai.abstrusewriting.homepage.hot.clipImage.CircleImage;
import org.daidai.abstrusewriting.homepage.hot.model.DiscoveryColumns;
import org.daidai.abstrusewriting.homepage.hot.model.EditorRecommendAlbums;
import org.daidai.abstrusewriting.homepage.hot.model.Guess;
import org.daidai.abstrusewriting.homepage.hot.model.GuessItem;
import org.daidai.abstrusewriting.homepage.hot.model.RecommendAlbums;
import org.daidai.abstrusewriting.homepage.hot.model.SpecialColumn;
import org.daidai.abstrusewriting.homepage.top.TopListActivity;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by Administrator on 2016/10/31.
 */

public class MyCommonAdapter extends RecyclerView.Adapter {
    private List list;
    private static Context context;

    public MyCommonAdapter(List list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder ret = null;
        View view = null;
        switch (viewType){
            case 1: view = LayoutInflater.from(context).inflate(R.layout.item_discovery_scroll,parent,false);
                    ret = new DiscoveryColumnsViewHolder(view);
                break;
            case 2: view = LayoutInflater.from(context).inflate(R.layout.item_guesslike_item,parent,false);
                    ret = new GuessViewHolder(view);
                break;
            case 3: view = LayoutInflater.from(context).inflate(R.layout.item_item_common,parent,false);
                    ret = new EditorRecommendAlbumsViewHolder(view);
                break;
            case 4:view = LayoutInflater.from(context).inflate(R.layout.item_listening_list,parent,false);
                   ret = new SpecialColumnViewHolder(view);
                break;
        }
        return ret;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Object o = list.get(position);
        if (holder instanceof DiscoveryColumnsViewHolder){
            DiscoveryColumnsViewHolder discoveryColumnsViewHolder = (DiscoveryColumnsViewHolder) holder;
            DiscoveryColumns discoveryColumns = (DiscoveryColumns) o;
            discoveryColumnsViewHolder.bindView(discoveryColumns);
        }else if(holder instanceof GuessViewHolder){
            GuessViewHolder guessViewHolder = (GuessViewHolder) holder;
            Guess guess = (Guess) o;
            guessViewHolder.bindView(guess);
        }else if (holder instanceof EditorRecommendAlbumsViewHolder){
            EditorRecommendAlbumsViewHolder editorRecommendAlbumsViewHolder = (EditorRecommendAlbumsViewHolder) holder;
            EditorRecommendAlbums editorRecommendAlbums = (EditorRecommendAlbums) o;
            editorRecommendAlbumsViewHolder.bindView(editorRecommendAlbums);
        }else if (holder instanceof SpecialColumnViewHolder){
            SpecialColumnViewHolder specialColumnViewHolder = (SpecialColumnViewHolder) holder;
            SpecialColumn specialColumn = (SpecialColumn) o;
            specialColumnViewHolder.bindView(specialColumn);
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public static class CommonItemHolder extends RecyclerView.ViewHolder{
        public CommonItemHolder(View itemView) {
            super(itemView);
        }
    }

    @Override
    public int getItemViewType(int position) {
        int ret=0;
        Object o = list.get(position);
        if (o instanceof DiscoveryColumns){
            ret = 1;
        }else if (o instanceof Guess){
            ret = 2;
        }else if (o instanceof EditorRecommendAlbums){
            ret = 3;
        }else if (o instanceof SpecialColumn){
            ret = 4;
        }
        return ret;
    }

    public static class DiscoveryColumnsViewHolder extends RecyclerView.ViewHolder{

        public DiscoveryColumnsViewHolder(View itemView) {
            super(itemView);
        }
        public void bindView(DiscoveryColumns discoveryColumns){
            List<DiscoveryColumns.DetailColumns> columnses = discoveryColumns.getColumnses();
            for (int i = 0; i < 7; i++) {
                ImageView imageView = (ImageView) getChildView("discovery_item_imageview" + i);
                DiscoveryColumns.DetailColumns detailColumns = columnses.get(i);
                Picasso.with( imageView.getContext())
                        .load(detailColumns.getCoverPath())
                        .config(Bitmap.Config.ARGB_8888)
                        .resize(75,75)
                        .into(imageView);
                imageView.setTag(detailColumns);
                TextView textView = (TextView) getChildView("discovery_item_title" + i);
                textView.setText(detailColumns.getTitle());
            }
        }
        View getChildView(String name){
            View ret = null;
            Class<R.id> aClass = R.id.class;
            Field field = null;
            try {
                field = aClass.getDeclaredField(name);
                int id=0;
                field.setAccessible(true);
                id = field.getInt(aClass);
                if (id!=0) {
                    ret = itemView.findViewById(id);
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return ret;
        }
    }

    public static class GuessViewHolder extends RecyclerView.ViewHolder{

        public GuessViewHolder(View itemView) {
            super(itemView);
        }
        public void bindView(Guess guess){
            TextView title = (TextView) getChildView("item_guess_title");
            title.setText(guess.getTitle());
            List<GuessItem> guessItems = guess.getGuessItems();
            for (int i = 0; i < 6; i++) {
                GuessItem guessItem = guessItems.get(i);
                ImageView imageView = (ImageView) getChildView("item_guess_imageview" + i);

                Picasso.with(imageView.getContext())
                        .load(guessItem.getCoverMiddle())
                        .config(Bitmap.Config.ARGB_8888)
                        .into(imageView);
                imageView.setTag(guessItem);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Object tag = v.getTag();
                        if (tag instanceof GuessItem) {
                            GuessItem guessItem = (GuessItem) tag;
                            Intent intent = new Intent(context, EditRecommendActivity.class);
                            intent.putExtra("data", guessItem);
                            context.startActivity(intent);
                        }
                    }
                });
                TextView mtitle = (TextView) getChildView("item_guess_title" + i);
                mtitle.setText(guessItem.getTrackTitle());
                TextView mtip = (TextView) getChildView("item_guess_tip" + i);
                mtip.setText(guessItem.getTitle());
            }

        }
        View getChildView(String name){
            View ret = null;
            Class<R.id> aClass = R.id.class;
            Field field = null;
            try {
                field = aClass.getDeclaredField(name);
                int id=0;
                field.setAccessible(true);
                id = field.getInt(aClass);
                if (id!=0) {
                    ret = itemView.findViewById(id);
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return ret;
        }
    }

    public static class EditorRecommendAlbumsViewHolder extends RecyclerView.ViewHolder{

        public EditorRecommendAlbumsViewHolder(View itemView) {
            super(itemView);
        }
        public void bindView(EditorRecommendAlbums editorRecommendAlbums){
            TextView title = (TextView) getChildView("item_common_title");
            title.setText(editorRecommendAlbums.getTitle());
            List<RecommendAlbums> recommendItem = editorRecommendAlbums.getRecommendItem();
            for (int i = 0; i < 3; i++) {
                RecommendAlbums recommendAlbums = recommendItem.get(i);
                ImageView imageview = (ImageView) getChildView("item_common_imageview"+i);
                Picasso.with(imageview.getContext())
                        .load(recommendAlbums.getCoverMiddle())
                        .config(Bitmap.Config.ARGB_8888)
                        .into(imageview);
                imageview.setTag(recommendAlbums);
                imageview.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            Object tag = v.getTag();
                            if (tag instanceof RecommendAlbums){
                                RecommendAlbums recommendAlbums = (RecommendAlbums) tag;
                                Intent intent = new Intent(context, EditRecommendActivity.class);
                                intent.putExtra("data",recommendAlbums);
                                context.startActivity(intent);
                            }
                    }
                });
                TextView mtitle = (TextView) getChildView("item_common_title"+i);
                mtitle.setText(recommendAlbums.getTrackTitle());

                TextView mtip = (TextView) getChildView("item_common_tip"+i);
                mtip.setText(recommendAlbums.getTitle());

            }

        }
        View getChildView(String name){
            View ret = null;
            Class<R.id> aClass = R.id.class;
            Field field = null;
            try {
                field = aClass.getDeclaredField(name);
                int id=0;
                field.setAccessible(true);
                id = field.getInt(aClass);
                if (id!=0) {
                    ret = itemView.findViewById(id);
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return ret;
        }

    }
    public static class SpecialColumnViewHolder extends RecyclerView.ViewHolder{

        public SpecialColumnViewHolder(View itemView) {
            super(itemView);
        }
        public void bindView(SpecialColumn specialColumn){
            TextView title = (TextView) getChildView("listen_item_title");
            title.setText(specialColumn.getTitle());
            List<SpecialColumn.Special> specials = specialColumn.getSpecials();
            for (int i = 0; i < 2; i++) {
                ImageView imageView = (ImageView) getChildView("listen_item_image" + i);
                Picasso.with(imageView.getContext())
                        .load(specials.get(i).getCoverPath())
                        .config(Bitmap.Config.ARGB_8888)
                        .into(imageView);
                TextView mtitle = (TextView) getChildView("listen_item_title" + i);
                mtitle.setText(specials.get(i).getTitle());
                TextView meditname = (TextView) getChildView("listen_item_editname" + i);
                meditname.setText(specials.get(i).getSubtitle());
                TextView mtip = (TextView) getChildView("listen_item_tip" + i);
                mtip.setText(specials.get(i).getFootnote());
            }

        }

        View getChildView(String name){
            View ret = null;
            Class<R.id> aClass = R.id.class;
            Field field = null;
            try {
                field = aClass.getDeclaredField(name);
                int id=0;
                field.setAccessible(true);
                id = field.getInt(aClass);
                if (id!=0) {
                    ret = itemView.findViewById(id);
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return ret;
        }
    }
}
